package net.fdukedom.epicurus.presentation.actions.user;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;

import net.fdukedom.epicurus.presentation.actions.AbstractForm;
import net.fdukedom.epicurus.presentation.SessionData;
import net.fdukedom.epicurus.domain.entity.User;

/**
 * Bean for user profile form.
 *
 * @author Alexander Kirakozov
 * @author Dmitry Y. Kochelaev
 */
public class ProfileForm extends AbstractForm {

    /**
     * User name.
     */
    private String userName;

    /**
     * E-mail.
     */
    private String email;

    /**
     * Password.
     */
    private String password;

    /**
     * User new repeat.
     */
    private String passwordNew;

    /**
     * User password repeat.
     */
    private String passwordRepeat;

    /**
     * User sex.
     */
    private Short sex;

    @Override
    public void doReset(ActionMapping mapping, HttpServletRequest request) {
        User user = SessionData.getInstance(request).getUser();
        userName = user.getName();
        email = user.getEmail();
        password = "";
        passwordNew = "";
        passwordRepeat = "";
        sex = user.getSex();
    }

    @Override
    public ActionErrors doValidate(ActionMapping mapping,
                                   HttpServletRequest request) {
        ActionErrors errors = new ActionErrors();

        if (userName.length() == 0) {
            errors.add("error.profile.login", new ActionMessage(
                    "profile.errors.emptyLogin"));
        }
        if (password.length() != 0 || passwordNew.length() != 0
                || passwordRepeat.length() != 0) {
            if (password.length() == 0) {
                errors.add("error.profile.password", new ActionMessage(
                        "profile.errors.emptyPassword"));
            } else {
                User user = SessionData.getInstance(request).getUser();

                if (passwordNew.length() == 0) {
                    errors.add("error.profile.passwordNew",
                            new ActionMessage(
                                    "profile.errors.emptyPassword"));
                }

                if (user.getPassword().equals(User.hash(password))) {
                    if (!passwordNew.equals(passwordRepeat)) {
                        errors.add("error.profile.passwordRepeat",
                                new ActionMessage(
                                        "profile.errors.differentNewPassword"));
                    }
                } else {
                    errors.add("error.profile.password", new ActionMessage(
                            "profile.errors.incorrectPassword"));
                }
            }
        }
        return errors;
    }

    /**
     * Returns user name.
     *
     * @return root login
     */
    public String getUserName() {
        return userName;
    }

    /**
     * Sets user name.
     *
     * @param userName user name
     */
    public void setUserName(String userName) {
        this.userName = userName;
    }

    /**
     * Returns user email.
     *
     * @return user email
     */
    public String getEmail() {
        return email;
    }

    /**
     * Empty method. Just for compatibility with bean interface. It does not
     * modify user entity anyhow.
     *
     * @param email user email
     */
    public void setEmail(String email) {
    }

    /**
     * Returns empty string. The password is not to be shown on profile page.
     *
     * @return empty string
     */
    public String getPassword() {
        return "";
    }

    /**
     * Returns empty string. The password is not to be shown on profile page.
     *
     * @return empty string
     */
    public String getPasswordNew() {
        return "";
    }

    /**
     * Returns empty string. The password is not to be shown on profile page.
     *
     * @return empty string
     */
    public String getPasswordRepeat() {
        return "";
    }

    /**
     * Returns user password.
     *
     * @return user password
     */
    public String getNonEmptyPassword() {
        return password;
    }

    /**
     * Returns new password.
     *
     * @return new password
     */
    public String getNonEmptyPasswordNew() {
        return passwordNew;
    }

    /**
     * Returns repeated password.
     *
     * @return repeated password
     */
    public String getNonEmptyPasswordRepeat() {
        return passwordRepeat;
    }

    /**
     * Sets user password.
     *
     * @param password user password
     */
    public void setPassword(String password) {
        this.password = password;
    }

    /**
     * Sets user new password.
     *
     * @param passwordNew user new password
     */
    public void setPasswordNew(String passwordNew) {
        this.passwordNew = passwordNew;
    }

    /**
     * Sets repeated password.
     *
     * @param passwordRepeat user password
     */
    public void setPasswordRepeat(String passwordRepeat) {
        this.passwordRepeat = passwordRepeat;
    }

    /**
     * Returns user sex.
     *
     * @return user sex
     */
    public Short getSex() {
        return sex;
    }

    /**
     * Sets user sex.
     *
     * @param sex user sex
     */
    public void setSex(Short sex) {
        this.sex = sex;
    }
}
